
#include <Wire.h>
#include <Time.h>     // Date & Time
#include <LiquidCrystal_I2C.h>

#include "debug.h"
#include "lcd.h"

//
// Set the LCD address to 0x27 for a 16 chars and 2 line display
//
static LiquidCrystal_I2C lcd(0x27, 16, 2);

#define LCD_ERROR_NONE  0x00
#define LCD_ERROR_TIME  0x01   // Time not set
#define LCD_ERROR_SYNC  0x02   // Time out of sync

static uint8_t error;

// ===================================================================
//   LCD_Print_Weekday
// ===================================================================
static void LCD_Print_Weekday ()
{
  int w = weekday();         // the weekday now (Sunday is day 1) 

  lcd.print(dayStr(w));

#if 0
  switch (day) {
  case MONDAY: lcd.print(F("MON")); break;
  case TUESDAY: lcd.print(F("TUE")); break;
  case WENDESDAY: lcd.print(F("WED")); break;
  case THURSDAY: lcd.print(F("THU")); break;
  case FRIDAY: lcd.print(F("FRI")); break;
  case SATURDAY: lcd.print(F("SAT")); break;
  case SUNDAY: lcd.print(F("SUN")); break;
  }
#endif // 0
  return;
}

// ===================================================================
//   LCD_Print_Time
// ===================================================================
static void LCD_Print_Time ()
{
  int h = hour();
  int m = minute();

  if (h < 10) {
    lcd.print(F("0"));
  }
  lcd.print(h,DEC);

  lcd.print(F(":"));

  if (m < 10) {
    lcd.print(F("0"));
  }
  lcd.print(m,DEC);
}

#if 0
// ===================================================================
//   processSyncMessage
//
//   Synchronize time from time to time
// ===================================================================
void processSyncMessage () 
{
  time_t time = 0;

  setTime(time);   // Sync Arduino clock to the time received
}
#endif

extern "C" {

// ===================================================================
//   LCD_Display_Error
// ===================================================================
void LCD_Display_Error (int8_t err)
{
  lcd.setCursor(0,0);
  lcd.print(F("ERR: Fatal !"));
  lcd.setCursor(0,1);
  lcd.print(F("----------------"));
}

// ===================================================================
//   LCD_Display_Root
// ===================================================================
void LCD_Display_Root (void)
{
  lcd.setCursor(0,0);
  lcd.print(F("N "));

  if (timeStatus() == timeNeedsSync) {
    lcd.print(F("ERR: Time Sync !"));
  }

  else if (error == LCD_ERROR_TIME) {
    lcd.print(F("ERR: Set Time!"));
  }

  else {
    LCD_Print_Weekday();
    lcd.print(F("      "));
    LCD_Print_Time ();
  }

  lcd.setCursor(0,1);
  lcd.print(F("EXT:-"));
  lcd.print(5);
  lcd.print(F("C"));
  lcd.print(F(" INT:"));
  lcd.print(20);
  lcd.print(F("C"));
}

// ===================================================================
//   LCD_Display_Menu
// ===================================================================
void LCD_Display_Menu (void)
{
  lcd.setCursor(0,0);
  lcd.print(F(" N "));
  lcd.print(F(" R "));
  lcd.print(F(" H "));
  lcd.print(F(" M "));
  lcd.print(F(" W "));

  lcd.setCursor(0,1);
  lcd.print(F(" | "));
}

// ===================================================================
//   LCD_setup
// ===================================================================
void LCD_setup (void)
{
  error = LCD_ERROR_NONE;

  //
  // LCD dimentions
  //
  //lcd.begin(16,2);
  //lcd.clear();      // this clears the LCD. You can use this at any time
  //lcd.print(F("Yes it works!"));

  //lcd.init();                          // Init the display, clears the display
  // initialize the LCD
  lcd.begin();
  // Turn on the blacklight and print a message.
  lcd.backlight();

  //lcd.print(F("HELLO"));

  //
  // Set some default time, so display can work.
  // But mark out-of-sync until sync'd
  //
  setTime(0,0,0,1,1,2014);
  //error = LCD_ERROR_TIME;

#if 0
  //
  // Sync time via ethernet: http://playground.arduino.cc/Code/Time
  //
  Ethernet.begin(mac,ip,gateway);  
  PRINTF ("waiting for sync");
  setSyncProvider(getNtpTime);
  while(timeStatus()== timeNotSet) {
     ; // wait until the time is set by the sync provider
  }
#endif // 0

}

}
